package com.nirvana.utils.security;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESTools {

	public static String encrypt(String sSrc, String sKey) {
		if(sKey == null) {
			throw new IllegalArgumentException("key should not be null");
		}
		
		if (sKey.length() != 16) {
			throw new IllegalStateException("key should be 16 bit.");
		}
		
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw,"AES");
		byte[] encrypted = null;
		try {			
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
			IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);			
	        encrypted = cipher.doFinal(sSrc.getBytes());
		} catch (NoSuchAlgorithmException e) {
			//never happened.
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			//never happened.
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			//never happened.
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			//never happened.
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			//never happened.
			e.printStackTrace();
		} catch (BadPaddingException e) {
			//never happened.
			e.printStackTrace();
		}
		return new BASE64Encoder().encode(encrypted);
	}
	
	// 解密
	public static String decrypt(String sSrc, String sKey) throws IOException{
    	if(sKey == null) {
			throw new IllegalArgumentException("key should not be null");
		}
		
		if (sKey.length() != 16) {
			throw new IllegalStateException("key should be 16 bit.");
		}
		byte[] raw;
		String originalString = null;
		try {
			raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
	        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
	        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
	        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
	        byte[] encrypted = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
	        System.out.println("length: " + encrypted.length);
	        byte[] original = cipher.doFinal(encrypted);
	        originalString = new String(original);	        
		} catch (UnsupportedEncodingException e) {
			//never happened.
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			//never happened.
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			//never happened.
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			//never happened.
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			//never happened.
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			//never happened.
			e.printStackTrace();
		} catch (BadPaddingException e) {
			//never happened.
			e.printStackTrace();
		} catch (IOException e) {
			throw e;
		}
		return originalString;
        
    }
	
	public static void main(String[] args) throws IOException {
		String cKey = "123456789abcde56";
		// 需要加密的字串
        String cSrc = "woncent8976@";
        System.out.println("明文: " + cSrc);
        
        //加密
        long lStart = System.currentTimeMillis();
        String enString = AESTools.encrypt(cSrc, cKey);
        System.out.println("加密后的： " + enString);
        long lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("加密耗时：" + lUseTime + "毫秒");
        
        // 解密
        lStart = System.currentTimeMillis();
        String DeString = AESTools.decrypt(enString, cKey);
        System.out.println("解密后的字串是：" + DeString);
        lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("解密耗时：" + lUseTime + "毫秒");
	}

}
